package ctu.edu.vn.musicmanager.business;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.Vector;

import org.apache.commons.codec.binary.Base64;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import ctu.edu.vn.musicmanager.accessdata.MusicORM;
import ctu.edu.vn.musicmanager.object.Music;

public class MusicCRUD {

	public final int file_deleted = -21;
	public final int file_not_found =-31;
	public final int success = 0;
	public final int overload_user = 2;
	public final int fail_writing = -12;
	private final String host_link = "C:\\xampp\\htdocs\\mymusic\\";
	
	ApplicationContext context;
	MusicORM msORM;
	Vector<Music> MusicList;
	int curIndex = 0;
	
	
	public MusicCRUD() {
		MusicList = new Vector<Music>();
		context = new ClassPathXmlApplicationContext("ctu/edu/vn/musicmanager/libraries/SpringHibernate.xml");
		msORM = (MusicORM) context.getBean("musicORMBean");
		getMusic();
	}

	public int getItemsCount() {
		return MusicList.size();
	}

	public String getNameAt(int index) {
		return MusicList.get(index).getName();
	}

	public int getIDAt(int index) {
		return MusicList.get(index).getID();
	}

	public String getTypeAt(int index) {
		return MusicList.get(index).getType();
	}

	public int addSong(String name, String type, String data) {
		int index = curIndex + 1;
		String filename = host_link +  index + "." + type;
		boolean res = writeFileString(data, filename);
		System.out.println(res);
		if (res == false)
			return fail_writing;
		Music ms = new Music();
		ms.setName(name);
		ms.setType(type);
		ms.setID(++curIndex);
		MusicList.add(ms);
		msORM.save(ms);
		return ms.getID();

	}

	public int editSong(int id, String newName) {
		int index = getIndex(id);
		if (index >= 0) {
			Music ms = new Music();
			ms.setID(id);
			ms.setName(newName);
			ms.setType(getTypeAt(index));
			System.out.println(id + " : " + newName + " : " + getTypeAt(index));
			MusicList.setElementAt(ms, index);
			msORM.update(ms);
			return success;
		}
		return file_deleted;
	}

	public int getIndex(int ID) {
		for (int i = 0; i < MusicList.size(); i++)
			if (getIDAt(i) == ID) {
				return i;
			}
		return -1;

	}

	public int getSongs(Vector<Integer> IDs, Vector<String> SongNames,
			Vector<String> SongFormats) {
		for (int i = 0; i < getItemsCount(); i++) {
			IDs.addElement(MusicList.get(i).getID());
			SongNames.addElement(MusicList.get(i).getName());
			SongFormats.addElement(MusicList.get(i).getType());
		}
		// Luôn luôn đúng
		return success;
	}

	public int deleteSong(int id) {
		int index = getIndex(id);
		if (index >= 0) {
			Music ms = new Music();
			new File(host_link + "" + id + "." + getTypeAt(index)).delete();
			MusicList.removeElementAt(index);
			ms.setID(id);
			msORM.delete(ms);
			return success;
		}
		return file_not_found; //Lỗi tập tin không tồn tại
	}


	public void getMusic() {
		for (Music music : msORM.fetchAllMusic()) {
			Music ms = new Music();
			ms.setID(music.getID());
			ms.setName(music.getName());
			ms.setType(music.getType());
			MusicList.addElement(ms);
			curIndex = Math.max(music.getID(), curIndex);
		}

	}
	private boolean writeFileString(String data, String aOutputFileName) {
		try {
			byte[] aInput = Base64.decodeBase64(data.getBytes());
			OutputStream output = null;
			output = new BufferedOutputStream(new FileOutputStream(
					aOutputFileName));
			output.write(aInput);
			output.close();
			return true;
		} catch (Exception ex) {
			return false;
		}
	}

}
